/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.util.validate;

import org.springframework.core.convert.converter.Converter;

import java.sql.Timestamp;

/**
 *
 * <p>CustomDateConverter</p>
 * <p>自定义日期转换器</p>
 * @author	zhx
 * @date	2016-3-20下午5:37:59
 * @version 1.0
 */
public class CustomDateConverter implements Converter<String, Timestamp> {

	public Timestamp convert(String timeStr) {
		// TODO Auto-generated method stub
			Timestamp t=null;
			long time=Long.valueOf(timeStr);
			t=new Timestamp(time);
		return t;
	}

	/*public Timestamp convert(String source) {
		// TODO Auto-generated method stub
		Timestamp t=null;
		String value = source.trim();
		if ("".equals(value)) {
			return null;
		}
		*//*if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
			source+=" 00:00:00";
		}*//*
            source=getTime(source);
			long time=Long.valueOf(source).longValue();
			t=new Timestamp(time);
		return t;
	}
	public static String getTime(String user_time) {
		String re_time = null;
		SimpleDateFormat sdf = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
		Date d;
		try {
			d = sdf.parse(user_time);
			long l = d.getTime();
			String str = String.valueOf(l);
			re_time = str.substring(0, 10);
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		return re_time;
	}*/
/*	private static final List<String> formarts = new ArrayList<String>(4);
	static{
		formarts.add("yyyy-MM");
		formarts.add(EnumUtils.DataFormat.Y_M_D.getValue());
		formarts.add("yyyy-MM-dd hh:mm");
		formarts.add(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
	}
	public Timestamp convert(String source) {
		String value = source.trim();
		if ("".equals(value)) {
			return null;
		}
		if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
			source+=" 00:00:00";
			return (Timestamp)parseDate(source, formarts.get(3));
		}else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
			return (Timestamp)parseDate(source, formarts.get(3));
		}else {
			throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
		}
	}
	*//**
	 * 功能描述：格式化日期
	 *
	 * @param dateStr
	 *            String 字符型日期
	 * @param format
	 *            String 格式
	 * @return Date 日期
	 *//*
	public  Date parseDate(String dateStr, String format) {
		Date date=null;
		try {
			DateFormat dateFormat = new SimpleDateFormat(format);
			date = (Date) dateFormat.parse(dateStr);
		} catch (Exception e) {
		}
		return date;
	}*/

}
